package com.liuyanban.action.user;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.liuyanban.action.base.BaseAction;
import com.liuyanban.domain.user.User;
import com.liuyanban.service.user.UserService;
import com.liuyanban.utils.ValidateUtil;

@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction<User> {
	@Resource(name = "userService")
	private UserService userService;

	private String ensurepassword;

	public void setEnsurepassword(String ensurepassword) {
		this.ensurepassword = ensurepassword;
	}

	public String getEnsurepassword() {
		return ensurepassword;
	}

	public String register() {
		return addUI;
	}

	public String add() {
		User user = new User();
		if (ValidateUtil.isNull(this.getModel().getUsername())
				|| ValidateUtil.isNull(this.getModel().getUserpassword())
				|| ValidateUtil.isNull(this.ensurepassword)) {
			this.addActionError("用户名或密码或确认密码不能为空");
			return addUI;
		}

		if (!this.getModel().getUserpassword().equals(this.ensurepassword)) {
			this.addActionError("两次输入密码不一致，请重新输入");
			return addUI;
		}
		BeanUtils.copyProperties(this.getModel(), user);
		this.userService.saveEntry(user);
		return login;
	}

}
